//
//  AppProduction.swift
//  LYOnlineBanking
//
//  Created by 李永彬 on 2025/7/20.
//

import UIKit

enum AppCountryEnv: String, RawRepresentable {
    case unknown
    case indonesia = "290"
    case india = "292"
    init?(rawValue: RawValue) {
        switch rawValue {
        case "290":
            self = .indonesia
        case "292":
            self = .india
        default:
            self = .unknown
        }
    }
    
    var numberCode: String {
        switch self {
        case .indonesia:
            return "62"
        case .india, .unknown:
            return "91"
        }
    }
}

var appCountryEnv: AppCountryEnv {
    guard let countryId = LYUserDefaults.shared.getString(forKey: countryIdKey) else { return .unknown }
    let env = AppCountryEnv(rawValue: countryId)
    return env ?? .unknown
}

//private func currentLanguage() -> String {
//    // 获取当前语言设置
//    guard let preferredLang = Locale.preferredLanguages.first else {
//        return "en-US"
//    }
//    
//    // 根据接口文档要求映射语言代码
//    let langMap = [
//        "zh-Hans": "zh-CN", // 简体中文
//        "zh-Hant": "zh-TW", // 繁体中文
//        "en": "en-US",      // 英文
//        "id": "id-ID"      // 印尼语
//    ]
//    
//    for (key, value) in langMap {
//        if preferredLang.hasPrefix(key) {
//            return value
//        }
//    }
//    
//    return "en-US" // 默认返回英文
//}

/// 自定义本地化方法，优先匹配当前语言，失败时回退到英文，最后返回原始 key
func LYlocalizedString(_ key: String, comment: String = "") -> String {
    // 1. 尝试获取当前语言的翻译
    var currentLanguage = "en"
    if appCountryEnv == .indonesia {
        currentLanguage = "id"
    }

    if let currentLanguagePath = Bundle.main.path(forResource: currentLanguage, ofType: "lproj"),
       let currentLanguageBundle = Bundle(path: currentLanguagePath) {
        let localizedString = currentLanguageBundle.localizedString(forKey: key, value: key, table: nil)
        if localizedString != key { // 找到了当前语言的翻译
            return localizedString
        }
    }
    
    // 2. 当前语言未找到，尝试回退到英文
    if let englishPath = Bundle.main.path(forResource: "en", ofType: "lproj"),
       let englishBundle = Bundle(path: englishPath) {
        let englishString = englishBundle.localizedString(forKey: key, value: key, table: nil)
        if englishString != key { // 找到了英文翻译
            return englishString
        }
    }
    
    // 3. 英文也没有，返回原始 key（避免显示空值）
    return key
}

var idfv: String {
    return UIDevice.current.identifierForVendor?.uuidString ?? ""
}

func setCustomTabBarHidden(_ hidden: Bool) {
    guard let window = UIApplication.shared.currentKeyWindow else {
//        assertionFailure("无法获取当前key window")
        return
    }
    
    if let mainVC = window.rootViewController as? LYMainViewController {
        mainVC.setCustomTabBarHidden(hidden)
    }
}

let xScale = SCREEN_WIDTH / 375.0
